package swta.hellolift.snippet

import _root_.net.liftweb.util._
import _root_.net.liftweb.http._
import _root_.net.liftweb.mapper._
import _root_.net.liftweb.util.Helpers._
import _root_.net.liftweb.sitemap._
import _root_.scala.xml._
import _root_.swta.hellolift.model.Entry

class BlogUtil {
  def entry = (new Entry).toForm(Full("Post"),
				 (t: Entry) => {
				   t.save
				   S.redirectTo("/view?id=" + t.id)})

  def viewentry(xhtml : Group) : NodeSeq = {
    val t = Entry.find(S.param("id"))
    t.map(t =>
      bind("entry", xhtml,
	   "name" -> t.title.toString,
	   "body" -> t.body.toString)) openOr <span>Not found!</span>
  }

  def _entryview(e : Entry) : Node = {
    <div>
    <strong>{e.title}</strong><br />
    <span>{e.body}</span>
    </div>
  }

  def viewblog(xhtml : Group) : NodeSeq = {
    val t = Entry.findAll
    
    t match {
      // If no 'id' was requested, then show a listing of all users.
      case Nil => <span><a href={"/blog?id=" + 0}>
				      {"Hans" + " " + "Wurst"}</a>
				      <br /></span>
      case entries => <span>{entries.flatMap(e => _entryview(e))}</span>
    }
  }
}
